package com.qingkai.onlineshop.presentation.dashboard

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.GridItemSpan
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.input.nestedscroll.nestedScroll
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.qingkai.onlineshop.R
import com.qingkai.onlineshop.app.Route
import com.qingkai.onlineshop.data.AppDatabase
import com.qingkai.onlineshop.ui.theme.OnLineShopAppTheme
import org.koin.compose.viewmodel.koinViewModel

/**
 *作者：created by 89270 on 2025/6/4 09:38
 *描述：
 */
@Composable
private fun injectPreviewModel(): DashboardViewModel {
    val context = LocalContext.current
    return remember { DashboardViewModel(AppDatabase.getInstance(context = context)) }
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun DashboardScreen(isPreview: Boolean = false, navigateToRoute: (Route) -> Unit) {
    val viewModel = if (isPreview) injectPreviewModel() else koinViewModel()
    val state by viewModel.state.collectAsStateWithLifecycle()
    val enterAlwaysScrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior()
    Scaffold(
        modifier = Modifier
            .fillMaxSize()
            .nestedScroll(enterAlwaysScrollBehavior.nestedScrollConnection), topBar = {
            DashboardTopBar(
                modifier = Modifier.padding(horizontal = 16.dp),
                scrollBehavior = enterAlwaysScrollBehavior
            )
        }, bottomBar = { DashboardBottomBar() }) { innerPadding ->
        LazyVerticalGrid(
            modifier = Modifier
                .fillMaxSize()
                .padding(innerPadding),
            columns = GridCells.Fixed(2),
        ) {
            item(span = { GridItemSpan(2) }) {
                SearchBar(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 16.dp)
                )
            }
            item(span = { GridItemSpan(2) }) {
                BannerList(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp),
                    banners = if (isPreview) testBanners else state.banners
                )
            }
            item(span = { GridItemSpan(2) }) {
                Text(
                    text = stringResource(R.string.str_product),
                    style = MaterialTheme.typography.bodyLarge,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.padding(horizontal = 16.dp)
                )
            }
            item(span = { GridItemSpan(2) }) {
                CategoryList(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(vertical = 16.dp),
                    categories = if (isPreview) testCategories else state.categories,
                    navigateToRoute = navigateToRoute
                )
            }
            item(span = { GridItemSpan(2) }) {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(start = 16.dp),
                    horizontalArrangement = Arrangement.SpaceBetween,
                    verticalAlignment = Alignment.CenterVertically,
                ) {
                    Text(
                        text = stringResource(R.string.str_popular_product),
                        style = MaterialTheme.typography.bodyLarge,
                        fontWeight = FontWeight.Bold,
                    )
                    TextButton(onClick = {}) {
                        Text(text = stringResource(R.string.str_see_all))
                    }
                }
            }
            items(if (isPreview) List(20) { testItemWithPics } else state.itemFulls) {
                ProductItem(
                    itemWithPics = it,
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(horizontal = 8.dp, vertical = 16.dp)
                        .clickable {
                            navigateToRoute(Route.Detail(it.item.id))
                        }
                )
            }
        }
    }
}

@Preview(showBackground = true)
@Composable
private fun DashboardScreenPreview() {
    OnLineShopAppTheme { DashboardScreen(isPreview = true, navigateToRoute = { }) }
}